var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var underscore = require("underscore");

/***
 * 自定义
 */
var util = require('../util/curl_util')
var log_configure = require('../log_configure')

//通过configure()配置log4js
log_configure.configure(app)
//引用bodyParser 这个不要忘了写
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({extended: true})); // for parsing application/x-www-form-urlencoded
//设置跨域访问
app.all('*', function (req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By", ' 3.2.1');
    res.header("Content-Type", "application/json;charset=utf-8");
    next();
});

app.get('/', function (req, res) {
    let json = {}
    json["a"] = req.query
    res.json(json)
});
app.post('/postCurlToModel', function (req, res) {
    //首先校验参数
    let curlCommond = req.body.curlCommond
    res.status(200)
    let json = {}
    json["curlCommond"] = curlCommond
    if (!underscore.isEmpty(curlCommond)) {
        try {
            json["curlModel"] = util.parseCurlCommand(curlCommond);
        } catch (e) {
            json["curlModel"] = "";
            json["errMsg"] = e.toString();
        }
    }
    res.json(json)
})
//配置服务端口
var server = app.listen(24001, function () {
    var host = server.address().address;
    var port = server.address().port;
    console.log('Example app listening at http://%s:%s', host, port);
})